<?php


namespace app\api\controller;
use app\api\model\MemberInfoModel;
use app\api\model\MemberModel;
use service\UploadService as Upload;
use library\logic\Page;
use think\File;
use think\Request;
use \think\Db;

/**
 * @title   会员模块
 * @desc    获取会员信息模块
 * Class Member
 * @package app\api\controller
 */
class Member extends AuthController {

    /**
     * @title 获取会员信息
     */
    public function info() {
        $this->success('会员信息', $this->_userInfo);
        //TabNine::Config
    }

    /**
     * @title 获取会员积分或余额
     * @param string type credit1 积分/余额
     */
    public function credit() {
        $type = 'credit1';
        $credit = db('meetyou_member')->where('id', $this->_userInfo['id'])->value($type);
        $this->success($type, [
            'credit' => $credit
        ]);
    }

    /**
     * @title 获取会员认证信息
     * @param int mid - 会员ID，为空获取当前登录会员
     */
    public function verified() {
        $mid = input('mid');
        if (empty($mid)) $mid = $this->mid;

        $member = MemberModel::get($mid);
        $member_info = MemberInfoModel::get($mid);

        if ($member) {
            $this->success($mid, [
                'nickname'      => $member->nickname,
                'realname'      => $member->realname,
                'phone'         => $member->phone,
                'spare_phone'   => $member->spare_phone,
                'is_agent'      => intval($member->is_agent),
                'credit1'       => $member->credit1,
                'credit2'       => $member->credit2,
                'status'        => $member->status,
                'wechat'        => ($member_info) ? $member_info->wechat : '',
                'wechat_qrcode' => ($member_info) ? $member_info->wechat_qrcode : '',
                'alipay'        => ($member_info) ? $member_info->alipay : '',
                'alipay_qrcode' => ($member_info) ? $member_info->alipay_qrcode : '',
                'verified'      => ($member_info) ? $member_info->verified : 0,
                'comment'       => ($member_info) ? $member_info->comment : ''
            ]);
        } else {
            $this->error(0);
        }
    }

    /**
     * @title 提交会员认证信息
     * @method POST
     * @param string $wechat
     * @param File $wechat_qrcode
     * @param string $alipay
     * @param File $alipay_qrcode
     * @param string $realname
     * @param string $phone
     *
     */
    public function setVerified(Request $request) {
        $mid = $this->mid;
        if(empty($mid)) $this->error('无效的会员信息！');
        $wechat = $request->input('wechat');
        $alipay = $request->input('alipay');
        $realname = $request->input('realname');
        $phone = $request->input('phone');

        $member_info = MemberInfoModel::get($mid);
        if (empty($member_info)) {
            $member_info = new MemberInfoModel([
                'mid'    => $mid,
                'wechat' => $wechat,
                'alipay' => $alipay
            ]);
            $member_info->save();
        } else {
            $member_info->wechat = $wechat;
            $member_info->alipay = $alipay;
            $member_info->save();
        }

        $member = MemberModel::get($mid);
        if($member) {
            $member->realname = $realname;
            $member->phone = $phone;
            $member->save();
            $this->success('OK');
        }else{
            $this->error('无效的会员信息');
        }
    }

    /**
     * @title 上传认证图片
     * @param Request $request
     */
    public function upImage(Request $request) {
        $mid = $this->mid;
        $type = $request->post('type');
        $file = $request->file('file');

        if (!empty($file)) {
            $qrcode_update = Upload::image('file', 'verifie/' . $mid . '/' . $type . '/' . date('Ymd'));
            $path = $qrcode_update->filePath;
            $member_info = MemberInfoModel::get($mid);
            if (empty($member_info)) {
                $member_info = new MemberInfoModel([
                    'mid'             => $mid,
                    $type . '_qrcode' => media($path),
                    'careate_at'      => time()
                ]);
                $member_info->save();
                $this->success(media($path));
            } else {
                $type_qrcode = $type . '_qrcode';
                $member_info->$type_qrcode = media($path);
                $member_info->update_at = time();
                $member_info->save();
                $this->success(media($path));
            }
        }
        return $this->error('请上传图片');

    }

    //用户扫描推荐码
    public function scan(){

        //获取登录用户的id
        $id = $this->userinfo['id'];
        var_dump($id);exit;
        $qrcode = createQrcode($id);
        if(empty($qrcode)){
            return $this->error('用户扫推荐码失败！');
        }
//        $this->success('用户扫推荐码成功！',$qrcode);
    }




    /**
     * @title 获取团队成员信息
     */

    public function peopleInfo(Request $request) {

        //获取登录用户的id
        $id =  $this->_userInfo['id'];
        //获取传过来的第几页
        $number = trim($request->get('page'));
        $psize = intval(input('psize', 15));
        $page = intval(input('page', $number));
        $offset = max(0, $page - $number) * $psize;
        $query =  Db::name('meetyou_member')
                  ->where(['parent_id'=> $id]);
        $info = $query->limit($offset, $psize)
            ->order('create_at', 'desc')
            ->field('realname,avatar,phone')
            ->select();
        $count = $query->count();
        $this->success($count, $info);
    }


}